
#include <QPainter>
#include "log.h"
#include "toolpencil.h"

ToolPencil::ToolPencil() : EditorTool("pencil")
{
}

ToolPencil::~ToolPencil()
{
}

void ToolPencil::mousePress(const QPoint& p)
{
	m_drawing = true;
	m_prev = p;
	mouseMove(p);
}

void ToolPencil::mouseRelease(const QPoint& p)
{
	m_drawing = false;
}

void ToolPencil::mouseMove(const QPoint& p)
{
	m_current = p;
	if(!m_drawing)
		return;
	QPainter painter(image());
	painter.save();
	painter.setPen(pen());
	painter.drawLine(m_prev, p);
	widget()->update(boundingRect(m_prev));
	m_prev = p;
	painter.restore();
}

QRect ToolPencil::boundingRect(const QPoint& p)
{
	return QRect(p.x() - pen().width() - 1, p.y() - pen().width() - 1, p.x() + pen().width() + 1, p.y() + pen().width() + 1);
}

QRect ToolPencil::draw(QPainter* painter)
{
	QRect r = boundingRect(m_current);

	painter->save();
	painter->setPen(pen());
	painter->drawPoint(m_current);
	painter->restore();
	
	return r;
}

